package com.baizan.daka.ui.dialog

import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.baizan.daka.util.AppUtils.Companion.dp2px
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.dianzan.daka.R


class TaskSelectDialog : PopupWindow {
    var context: Context
    var data: List<String>? = null

    var listener: BaseQuickAdapter.OnItemClickListener? = null
    var index: Int? =  null

    constructor(context: Context, data: List<String>, index: Int?) : super(context) {
        this.context = context
        this.data = data
        this.index = index
        init()
    }

    fun showPopupWindow(view: View) {
        val location = IntArray(2)
        view.getLocationOnScreen(location)
        //        int x =(location[0] + view.getWidth() / 2)  - DimenUtils.dp2px(3);
        val x: Int = location[0]
        val y: Int = location[1] + view.getHeight() + dp2px(10)
        showAtLocation(view, Gravity.NO_GRAVITY, 0, y)
    }

    private fun init() {
        val recyclerView = LayoutInflater.from(context).inflate(R.layout.popup_window_down, null)
        recyclerView as RecyclerView
        val mLinearLayoutManager = LinearLayoutManager(context)
        mLinearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        recyclerView.layoutManager = mLinearLayoutManager
        val adapter = Adapter(data)
        recyclerView.adapter = adapter
        // 为了避免部分机型不显示，我们需要重新设置一下宽高
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT)
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT)
        // 设置pop透明效果
        setBackgroundDrawable(ColorDrawable(0x0000))
        // 设置pop出入动画
//        setAnimationStyle(R.style.selectDialog)
        // 设置pop获取焦点，如果为false点击返回按钮会退出当前Activity，如果pop中有Editor的话，focusable必须要为true
        setFocusable(true)
        // 设置pop可点击，为false点击事件无效，默认为true
        setTouchable(true)
        // 设置点击pop外侧消失，默认为false；在focusable为true时点击外侧始终消失
        setOutsideTouchable(true)
        // 相对于 + 号正下面，同时可以设置偏移量

        // 设置pop关闭监听，用于改变背景透明度
//        setOnDismissListener(new PopupWindow.OnDismissListener() {
//            @Override
//            public void onDismiss() {
//
//            }
//        });
        setContentView(recyclerView)
    }


    inner class Adapter(data: List<String>?) :
        BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_popupwindow, data) {
        override fun convert(helper: BaseViewHolder, item: String) {
            var textViewView = helper.itemView
            textViewView as TextView
            textViewView.text = item
            index?.let {
                if (index == helper.adapterPosition) {
                    textViewView.isSelected = true
                }
            }

            textViewView.setOnClickListener {
                it.isSelected = true
                if (listener != null) {
                    listener!!.onItemClick(this, it, helper.adapterPosition)
                }
            }
        }
    }
}